/**
 * Copyright (C), 2015-2018, 真格邦软件科技有限公司
 * 文件名: Constants
 * 作者: lsyzx【892366102@qq.com】
 * 创建时间: 2018/11/20 14:19
 * 描述: TODO
 * 历史修改:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.zx.utils;

import com.alibaba.fastjson.JSON;
import com.zx.utils.vo.ApiResult;
import com.zx.utils.vo.ResultEnum;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.UUID;

/**
 * 作者: lsyzx【892366102@qq.com】
 * 创建时间: 2018/11/20 14:19
 * 描述: 常量类
 * 历史修改:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
public class Constants {

    /**
     * 流程定义启用
     */
    public static final int PROCESS_DEF_STATE_YES = 0;

    /**
     * 流程定义禁用
     */
    public static final int PROCESS_DEF_STATE_NO = 1;

    /**
     * 用户状态：未启用
     */
    public static final int USER_STATE_NO = 0;

    /**
     * 用户状态：启用
     */
    public static final int USER_STATE_YES = 1;

    /**
     * 时间格式化
     */
    public static SimpleDateFormat SIPLEDATEYYYYMMDDHHMMSS = new SimpleDateFormat("yyyyMMddHHmmss");

    /**
     * 获取本机的ip地址
     *
     * @return String
     */
    public static String getHostIP() {
        InetAddress addr;
        String ip = "";
        try {
            addr = InetAddress.getLocalHost();
            ip = addr.getHostAddress().toString(); //获取本机ip
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return ip;
    }


    /**
     * token Keys
     */
    public static final String TOKENSKEY = "tokens:";

    /**
     * token
     *
     * @return
     */
    public static String getToken(String key, String token) {
        return key + "_" + token;
    }

    /**
     * key
     *
     * @return
     */
    public static String getKey() {
        return TOKENSKEY + UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 设置响应头
     *
     * @param httpResponse
     */
    public static void setReponse(HttpServletResponse httpResponse, Boolean isFlag) throws IOException {
        httpResponse.setHeader("Access-Control-Allow-Origin", "*");
        httpResponse.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
        httpResponse.setHeader("Access-Control-Max-Age", "3600");
        httpResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Cache-Control, Authorization");
        httpResponse.setCharacterEncoding("UTF-8");
        httpResponse.setContentType("application/json; charset=utf-8");
        if (isFlag) {
            httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            httpResponse.getWriter().write(JSON.toJSONString(new ApiResult(ResultEnum.AUTH_FAIL)));
        } else {
            httpResponse.setStatus(HttpServletResponse.SC_OK);
        }
    }
}
